<template>
	<view class="content">
		<view>
			<view>
				<view class="header"><image src="../../static/download.png" mode="aspectFill"></image></view>

				<view class="content11">
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像等)</text>
				</view>
				<button
					@click="back"
					type=""
					class="u-reset-button bottom"
					style="border: 1rpx solid #CCCCCC; height: 80rpx; display: flex; align-items: center; justify-content: center; font-size: 35rpx;"
				>
					取消
				</button>
				<button class="bottom" type="primary" open-type="getUserInfo" lang="zh_CN" @getuserinfo="onGotUserInfo">授权登录</button>
			</view>
		</view>
	</view>
</template>

<script>
import helper from '../../common/helper.js';
export default {
	data() {
		return {
			show: false,
		};
	},
	onLoad() {},
	methods: {
		back() {
			uni.switchTab({
				url: 'Home',
			});
		},
		async bind() {
			console.log(' uni.g :>> ', uni.getStorageSync('user_id'));
			let res;
			try {
				res = await this.$request.post('api/User/user_relation', {
					data: {
						agentid: uni.getStorageSync('user_id'),
					},
				});
			} catch (e) {
				//TODO handle the exception
				return uni.showToast({
					title: '网络连接失败！',
					icon: 'none',
				});
			}

			console.log('res **+*+*+*:>> ', res);
		},
		// 获取用户授权
		onGotUserInfo(e) {
			if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了授权！');

			this.getToken(e.detail);
		},
		// 注册
		async getToken(info) {
			// 获取code
			const [err, res] = await uni.login().catch(err => err);
			if (err || res.errMsg !== 'login:ok')
				return uni.showToast({
					title: '用户登录失败！',
					icon: 'none',
				});

			// 将用户数据保存在本地缓存
			uni.setStorageSync('info', info);

			//向后端进行注册
			let data;
			try {
				data = await this.$request.get('api/wxapp/Public/regist', {
					data: {
						code: res.code,
						encrypted_data: info.encryptedData,
						iv: info.iv,
					},
					header: {
						'XX-Wxapp-AppId': helper.appid,
					},
					dataType: 'json',
					responseType: 'text',
				});
			} catch (e) {
				//TODO handle the exception
				return uni.showToast({
					title: '网络连接失败！',
					icon: 'none',
				});
			}

			// 后端返回失败code
			if (data.data.code != 1) {
				console.log('data :>> ', data);
				return uni.showToast({
					title: '用户登录失败！',
					icon: 'none',
				});
			}

			// 用户数据保存在本地缓存
			uni.setStorageSync('token', data.data.data.token);
			uni.setStorageSync('session_key', data.data.data.session_key);
			uni.setStorageSync('user', data.data.data.user);
			uni.setStorageSync('id', data.data.data.user.id);

			helper.myToken = data.data.data.token;

			//绑定关系

			// 跳转首页
			this.$Router.replaceAll({
				name: 'home',
			});

			//信息提示
			if (data.data.code === 1) this.bind();

			return uni.showToast({
				title: '用户登录成功！',
				icon: 'none',
			});
		},
	},
};
</script>

<style lang="scss" scoped>
.content {
	width: 100vw;
	height: 100vh;
	background-color: #ffffff;
	overflow: hidden;

	.header {
		margin: 90rpx 0 90rpx 50rpx;
		// border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content11 {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content11 text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
}
</style>
